<template>
  <!-- 添加或修改项目基础信息管理对话框 -->
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body v-dialogDrag width="90%">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="产品编码" prop="goodsCode">
        <el-input v-model="queryParams.goodsCode" placeholder="请输入产品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品型号" prop="model">
        <el-input v-model="queryParams.model" placeholder="请输入产品型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div v-if="selectedRows.length > 0">
            <div>已勾选订单数量合计: {{selectedRows.reduce((acc, row) => acc + row.orderCount, 0)}}</div>
        </div>
    <el-table :data="columns" ref="dragTable" row-key="id" v-loading="loading" :summary-method="totalOutYear" @selection-change="handleSelectionChange" @sort-change="handleSortChange"
      show-summary>
      <el-table-column type="selection" width="100" />
      <el-table-column align="center" label="序号" type="index" width="50">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable align="center" label="产品编码" prop="goodsCode" />
      <el-table-column sortable align="center" label="产品名称" prop="goodsName" show-overflow-tooltip />
      <el-table-column sortable align="center" label="产品型号" prop="goodModel" />
      <el-table-column sortable align="center" label="订单数量" prop="orderCount" />
      <el-table-column sortable align="center" label="含税单价" prop="goodsUnitPrice" />
      <el-table-column sortable align="center" label="不含税单价" prop="goodsTaxUnitPrice" />
      <el-table-column sortable align="center" label="总价（不含税）" prop="goodsTotalPrice" />
      <el-table-column sortable align="center" label="总价（含税）" prop="goodsTaxTotalPrice" />
      <el-table-column sortable align="center" label="备注" prop="remark" />
    </el-table>
    <pagination :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total" @pagination="getList"
      v-show="total > 0" />
    <div class="dialog-footer" slot="footer">
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>

import { accAdd, addAction, deleteAction, downloadFile, getAction } from '@/api/manage'

export default {
  name: "GoodsOrderNoMergeList",
  dicts: ['isdo', 'sale_state', 'goods_status'],
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "订单产品明细",
      // 是否显示弹出层
      open: false,
      // 显示搜索条件
      showSearch: true,
      // 表单参数
      form: {},
      proId: "",
      proStatus: "",
      columns: [],
      total: 0,
      // 表单校验
      rules: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        code: null,
        projectNo: null,
        goodsName: null,
        goodsCode: null,
        model: null,
        brand: null,
        isdo: null,
        remark: null,
        projectId: null,
        //orderByColumn:"isdo",
        isAsc: "desc",
        params: {}
      },
      selectedRows:[],
      url: {
        listMergeByOrders: '/goodsSale/goodsSale/listNoMergeByOrders',
        exportData: '/goodsSale/goodsSale/exportNoMergeDataByOrders',
      },
    };
  },
  updated() {
    this.$nextTick(() => {
      // tab 绑定ref 属性
      this.$refs["dragTable"].doLayout();
    });
  },
  created() {
    //this.handleQuery();
  },
  methods: {
    getList() {
      this.loading = true;
      this.queryParams.projectId = this.proId;
      getAction(this.url.listMergeByOrders, this.queryParams).then(response => {
        this.columns = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    show(ids) {
      this.open = true;
      this.queryParams.params["orderIds"] = ids;
      this.getList();
    },

    handleExport() {
      downloadFile(this.url.exportData, this.queryParams);
    },

    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectedRows = selection;
    },
    totalOutYear(param) {
      const { columns, data } = param;
      const sums = [];

      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if (column.property === "pleaseBuyCount" || column.property === "orderCount") {

          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },

  }
};
</script>
